package com.example.myapplication.network

import com.example.myapplication.utils.DigitalUtil
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException
import kotlin.jvm.Throws
import kotlin.let
import kotlin.text.isNullOrEmpty

class HeaderInterceptor : Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        val builder = request.newBuilder()
        var path = request.url.let {
            val query = it.encodedQuery
            if (query.isNullOrEmpty()) {
                it.encodedPath
            } else {
                "${it.encodedPath}?${it.encodedQuery}"
            }
        }
        builder.header("token", DigitalUtil.generateJWT(path))
        request = builder.build()
        return chain.proceed(request)
    }

}